/**************************************************************************/
/*  Copyright (C) School of Software Engineering of USTC, 2013             
/*                                                                        
/*  FILE NAME: Dictionary.h                                                  
/*  PRINCIPAL AUTHOR: SA12226340(Song Zeyu)                                                                                                          
/*  LANGUAGE: C++                                                          
/*  TARGET ENVIRONMENT: Linux                                             
/*  DATE OF FIRST RELEASE: 2013-05-13                                    
/*  DESCRIPTION:the word in dictionary stores in the defined hash_map                  
/**************************************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <stdlib.h>
using namespace std;

/*
*Reference:http://blog.csdn.net/qll125596718/article/details/8306767
*/
class CDictionary
{
public:
	CDictionary();          //The default constructor,when define the class ,call this function automatically
	~CDictionary();         //The destructor
	int FindWord(string w); //Public interface,find the word of the dictionary in the hash
private:
	string strtmp;          //Read the dictionary every line
	string word;            //save the word
	map<string, int> wordhash; 
	map<string, int >::iterator worditer; 
	typedef pair<string, int> sipair;
};

/*
*define a wordhash and map the word of wordlexicon.txt to wordhash
*/
CDictionary::CDictionary()
{
	ifstream infile("wordlexicon.txt"); // open the wordlexicon.txt
	if (!infile.is_open()) // open failure
	{
		cerr << "Unable to open input file: " << "wordlexicon"
			<< " -- bailing out!" << endl;
		exit(-1);
	}
	while (getline(infile, strtmp, '\n')) 
	{
		istringstream istr(strtmp);
		istr >> word;                    //write the word of every line 
		wordhash.insert(sipair(word, 1)); 
	}
}
CDictionary::~CDictionary()
{
}

int CDictionary::FindWord(string w)
{
	if (wordhash.find(w) != wordhash.end())
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
